//
//  DFUserAccountModel.swift
//  FlyBlog
//
//  Created by FEI on 16/4/12.
//  Copyright © 2016年 FEI. All rights reserved.
//

import UIKit



class DFUserAccountModel: NSObject {
    static let shareInstance = DFUserAccountModel()

    private override init() {
        super.init()
        userAccount = loadUserAccount()
    }

    var userAccount:DFUserAccount?

    var userLogin:Bool {
        get {
            return userAccount != nil
        }
    }

    func loadAccessToken(code:String,loadAccessTokenCallback: (error:NSError?) -> ()) {
        let urlString = accessTokenURLString

        let parameters = [
            "client_id": client_id,
            "client_secret": client_secret,
            "grant_type": grant_type,
            "code": code,
            "redirect_uri": redirect_uri
        ]

        DFNetworkTool.shareInstance.POST(urlString, parameters: parameters, progress: nil, success: { (task, responsObject) -> Void in
            let result = responsObject as?[String:AnyObject]
            if let dic = result {
                self.userAccount = DFUserAccount(dict:dic)
                self.saveUserAccount()
//                NSKeyedArchiver.archiveRootObject(userAccount, toFile: accountPath)
                loadAccessTokenCallback(error: nil)
            }
            }) { (task, error) -> Void in
                loadAccessTokenCallback(error: nil)

        }
    }

    private func saveUserAccount() {
        if let usreAccount = self.userAccount{
            NSKeyedArchiver.archiveRootObject(usreAccount, toFile: accountPath)
        }
    }

    func loadUserAccount() -> DFUserAccount? {
        let userAccount = NSKeyedUnarchiver.unarchiveObjectWithFile(accountPath) as? DFUserAccount
        if let account = userAccount where account.expiresDate?.compare(NSDate()) == NSComparisonResult.OrderedDescending {
            return account
        }

        return nil
    }

    //获取用户信息
    func loadUserInfo(loadUserInfoCallback:(error: NSError?) -> ()){
        guard let access_token = userAccount?.access_token else {
            let error = DFNetworkErrorEnum.AccessTokenEmpty.error()
            loadUserInfoCallback(error: error)
            return
        }

        guard let uid = userAccount?.uid else {
            let error = DFNetworkErrorEnum.UidEmpty.error()
            loadUserInfoCallback(error: error)
            return
        }

        let urlString = "https://api.weibo.com/2/users/show.json"

        let parameters = ["access_token": access_token,
        "uid":uid
        ]

        DFNetworkTool.shareInstance.GET(urlString, parameters: parameters, progress: nil
            , success: { (task, responseObject) -> Void in
                DFPrint("加载用户信息成功")
                if let dic = responseObject as? [String: AnyObject] {
                self.userAccount?.screen_name = dic["screen_name"] as? String
                self.userAccount?.avatar_large = dic["avatar_large"] as? String
                self.saveUserAccount()
                loadUserInfoCallback(error: nil)
                }
            }) { (task, error) -> Void in
                loadUserInfoCallback(error: error)
        }
    }
}

